package com.inspiration.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inspiration.dao.TagMapper;
import com.inspiration.entity.R;
import com.inspiration.entity.Tag;
import com.inspiration.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author Vincent.jiao
 * @Date 2023/10/24 11:58
 */
@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    TagMapper tagMapper;

    @Autowired
    TagService tagService;

    /**
     * 列表
     */
    @RequestMapping("/all")
    public Object all(String tName, String sortorder, int pageNo, int pageSize, String sortname ) {
        return tagService.page(tName, sortorder, pageNo, pageSize, sortname);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Object list(String tName) {
        return tagService.listVO(tName);
    }

    /**
     * 新增
     */
    @RequestMapping("/insert")
    public void insert(Tag tag) {
        tagService.insert(tag);
    }


    /**
     * 新增
     */
    @RequestMapping("/insert/batch")
    public Object batchInsert(@RequestBody Tag[] tags) {
        List<String> list = new LinkedList<>();
        if(tags == null || tags.length == 0) {
            list.add("ok");
            return list;
        }

        for (int i = 0; i < tags.length; i++) {
            Tag tag = tags[i];
            tag.setT_name(tag.getT_name().trim());
            tag.setT_desc(tag.getT_desc().trim());

            list.add(String.valueOf(tagService.insert(tag)));
        }
        
        return list;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public void update(Tag tag) {
        LambdaUpdateWrapper<Tag> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(Tag::getT_name, tag.getT_name())
                .set(Tag::getT_desc, tag.getT_desc())
                .eq(Tag::getT_id, tag.getT_id());

        tagMapper.update(tag, updateWrapper);
    }

    /**
     * del
     */
    @RequestMapping("/del")
    public void del(Long tId) {
        tagMapper.delContentRelation(tId);
        tagMapper.deleteById(tId);
    }


    /**
     * 获取关联个数
     */
    @RequestMapping("/count/content")
    public Object contenCount(Long tId) {
        int contentCount = tagService.contentCount(tId);
        return R.ok("contentCount", contentCount);
    }

    @RequestMapping("/preview")
    public Object previewAllTag() {
        List<Tag> tagList = tagMapper.selectList(null);
        Map<String, List<String>> map = new LinkedHashMap<>();
        for (Tag tag : tagList ) {
            List<String> list = map.get(tag.getT_desc());
            if(list == null) {
                list = new LinkedList<>();
            }

            list.add(tag.getT_name());
            map.put(tag.getT_desc(), list);
        }

        return map;
    }
}






























